#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define PORT 12345
#define SERVER_ADDRESS "10.135.11.3"  // 服务器地址

struct User {
  char username[50];
  char password[50];
};

int main() {
  int client_socket;
  struct sockaddr_in server_addr;

  struct User user;
  strcpy(user.username, "zzz");
  strcpy(user.password, "123456");

  client_socket = socket(AF_INET, SOCK_STREAM, 0);
  if (client_socket == -1) {
    perror("Socket creation failed");
    exit(EXIT_FAILURE);
  }

  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(PORT);
  server_addr.sin_addr.s_addr = inet_addr(SERVER_ADDRESS);

  if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
    perror("Connection failed");
    exit(EXIT_FAILURE);
  }

  printf("Connected to server\n");

  // 发送用户名和密码进行认证
  send(client_socket, &user, sizeof(user), 0);

  char auth_result[1024];
  recv(client_socket, auth_result, sizeof(auth_result), 0);
  printf("%s\n", auth_result);

  char message[1024];

  while (1) {
    printf("Enter your message: ");
    fgets(message, sizeof(message), stdin);
    send(client_socket, message, strlen(message), 0);

    recv(client_socket, message, sizeof(message), 0);
    printf("Received response: %s\n", message);
  }

  close(client_socket);

  return 0;
}
